package com.gosophia.metadataEngine.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.gosophia.commons.exception.BusinessException;
import com.gosophia.commons.exception.ConcurrencyException;
import com.gosophia.commons.exception.ExceptionDetail;
import com.gosophia.metadataEngine.commons.DBUtil;
import com.gosophia.metadataEngine.dao.MetaDataFiledDao;
import com.gosophia.metadataEngine.dao.MetaDataTableDao;
import com.gosophia.metadataEngine.entity.DataSourceInfo;
import com.gosophia.metadataEngine.entity.MetaDataField;
import com.gosophia.metadataEngine.entity.MetaDataTable;
import com.gosophia.metadataEngine.service.MetaDataFieldService;
/**
 * 元数据字段impl
 * @author mayue
 *
 */
@Service("metaDataFieldService")
@Transactional
public class MetaDataFieldServiceImpl implements MetaDataFieldService {
    
    @Autowired
    private MetaDataTableDao metaDataTableDao;
    
    @Autowired
    private MetaDataFiledDao metaDataFiledDao;

    public void addMetaDataField(List<MetaDataField> metaDataFields)
            throws BusinessException {
        Assert.notNull(metaDataFields,"metaDataFields不能为null");
        for(MetaDataField m:metaDataFields){
            Assert.notNull(m.getMetaDataTable(),"metaDataTable不能为null");
            Assert.notNull(m.getMetaDataTable().getMetaDataTableId(),"metaDataTableId不能为null");
            Assert.isNull(m.getMetaDataFieldId(),"id必须为null");
            Long count = metaDataFiledDao.countNamedQueryResult("MetaDataFiled.getSameNameAndTableId", 
                    m.getColName(),m.getFieldNameCN(),m.getPrivateName(),m.getMetaDataTable().getMetaDataTableId());
            if(count>0){
                throw new BusinessException("在系统"+m.getMetaDataTable().getTableName()+"下存在重复名称/中文名称/属性名称的记录","JC2_YSJ_B_6");
            }
            metaDataFiledDao.save(m);
        }
    }

//    public List<MetaDataField> findMetaDataFieldByTableId(Long tableId) {
//        Assert.notNull(tableId,"tableId不能为null");
//        return  metaDataFiledDao.getByProperty("metaDataTable.metaDataTableId", tableId);
//    }
    public List<MetaDataField> findMetaDataFieldByTableId(Long tableId) {
        Assert.notNull(tableId,"tableId不能为null");
        return  metaDataFiledDao.getListByNamedQuery("MetaDataFiled.findMetaDataFieldByTableIdOrderByfieldOrder", tableId);
    }



    public List<MetaDataField> findSystemFieldByTableId(Long tableId) throws BusinessException{
        MetaDataTable mdt = metaDataTableDao.get(tableId);
        if(mdt!=null){
            DataSourceInfo dsi = mdt.getDataSourceInfo();
            String tableName = mdt.getTableName();
            List<MetaDataField> list ;
            try{
                list = DBUtil.readTableStructure(dsi, tableName);
                return list;
            }catch(BusinessException e){
                throw e;
            }
        }else{
        	throw new BusinessException("该元数据表不存在！", "JC2_YSJ_C_20");
        }
        
    }

    public void updateMetaDataField(MetaDataField metaDataField)
            throws BusinessException {
        Assert.notNull(metaDataField,"metaDataFields不能为null");
        MetaDataField mdf = metaDataFiledDao.get(metaDataField.getMetaDataFieldId());
        if(mdf == null){
            throw new ConcurrencyException("该元数据字段已被删除！",new ExceptionDetail("JC2_YSJ_C_5","该元数据字段已被删除！"));
         }
        if(!metaDataField.getVersion().equals(mdf.getVersion())){
            throw new ConcurrencyException("该元数据字段已被修改！",new ExceptionDetail("JC2_YSJ_C_6","该元数据字段已被修改"));
         }
        Long count = metaDataFiledDao.countNamedQueryResult("MetaDataField.findBySameNameNotTableIdAndId", 
                metaDataField.getColName(),metaDataField.getFieldNameCN(),metaDataField.getPrivateName(),metaDataField.getMetaDataTable().getMetaDataTableId(),metaDataField.getMetaDataFieldId());
        if(count>0){
            throw new BusinessException("元数据字段不能重复！","JC2_YSJ_B_7");
        }
        metaDataFiledDao.update(metaDataField);

    }
    public void removeMetaDataField(Long metaDataFieldId, Long version){
        Assert.notNull(metaDataFieldId,"id不能为null");
        Assert.notNull(version,"version不能为null");
        
        MetaDataField mdf = metaDataFiledDao.get(metaDataFieldId);
        if(mdf == null){
           throw new ConcurrencyException("该元数据字段已被删除！",new ExceptionDetail("JC2_YSJ_C_7","该元数据字段已被删除"));
        }
        if(!version.equals(mdf.getVersion())){
            throw new ConcurrencyException("该元数据字段已被修改！",new ExceptionDetail("JC2_YSJ_C_8","该元数据字段已被修改！"));
        }
        metaDataFiledDao.delete(metaDataFieldId, version);
        
    }
    public List<MetaDataField> findMetaDataFieldByTableIdDetailShow(Long tableId) {
        Assert.notNull(tableId,"tableId不能为null");
        return metaDataFiledDao.getListByNamedQuery("MetaDataFiled.getListByIdAndShowFlagDetail", tableId);
  
    }
    public List<MetaDataField> findMetaDataFieldByTableIdListShow(Long tableId) {
        Assert.notNull(tableId,"tableId不能为null");
        return metaDataFiledDao.getListByNamedQuery("MetaDataFiled.getListByIdAndShowFlagList", tableId);
    }

    public List<MetaDataField> findMetaDataFieldByTableIdListShowNoHidden(
            Long tableId) {
        Assert.notNull(tableId,"tableId不能为null");
        return metaDataFiledDao.getListByNamedQuery("MetaDataFiled.getListByIdAndShowFlagListNoHidden", tableId);
    }

	@Override
	public List<MetaDataField> findShowTitleItemsByTableId(Long metaDataTableId) {
		Assert.notNull(metaDataTableId,"metaDataTableId不能为null");
        return metaDataFiledDao.getListByNamedQuery("metaDataFiled.findShowTitleItemsByTableId", metaDataTableId);
	}

	@Override
	public String findPrimaryKeyName(Long metaDataTableId) {
		Assert.notNull(metaDataTableId,"metaDataTableId不能为null");
		MetaDataField field =  metaDataFiledDao.getUniqueByNamedQuery("metaDataFiledDao.findPrimaryKeyName", metaDataTableId);
		if(field !=null){
			return field.getPrivateName();
		}else{
			return null;
		}
	}

	@Override
	public List<MetaDataField> findDetailShowItemsByTableId(Long metaDataTableId) {
		Assert.notNull(metaDataTableId,"metaDataTableId不能为null");
        return metaDataFiledDao.getListByNamedQuery("metaDataFiled.findDetailShowItemsByTableId", metaDataTableId);
	}

	@Override
	public MetaDataField findMetaDataFieldByDataFieldId(Long metaDataFieldId) {
		Assert.notNull(metaDataFieldId,"metaDataFieldId不能为null");
		return metaDataFiledDao.get(metaDataFieldId);
	}

}
